package spms.front.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import spms.front.config.Invoker;

@Controller
public class UserController {

    String serverName = "SPMS-DATA";

    /**AJAX
     **/
    @PostMapping("getUserList")
    @ResponseBody
    public Object getUserList() {
        return Invoker.INSTANCE.invoke(serverName,"/user/findAll",null, JSONArray.class);
    }

    /**跳转菜单列表页面
     * */
    @GetMapping("/userList")
    public Object getUserList(@RequestParam(defaultValue = "0") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,Model model) {
        userList(pageNum,pageSize,model);
        return "userList";
    }

    @PostMapping("/userList")
    public Object userList(@RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, Model model) {

        MultiValueMap mapParam = new LinkedMultiValueMap();
        mapParam.add("pageNum",pageNum);
        mapParam.add("pageSize",pageSize);
        JSONObject page = (JSONObject) Invoker.INSTANCE.invoke(serverName,"/user/findPage",mapParam, JSONObject.class);
        model.addAttribute("page", page);
        return "userList::userList";
    }
}
